package com.euc.springbootframework.auth.config;

import com.euc.springbootframework.auth.JWTAuthenticationFilter;
import com.euc.springbootframework.auth.JWTLoginFilter;
import com.euc.springbootframework.auth.JwtUrlSecurityInterceptor;
import com.euc.springbootframework.auth.manager.MyAuthenticationManager;
import com.euc.springbootframework.auth.service.TokenAuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutHandler;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }

    //这个拦截器用来实现按照用户权限，对所请求的url进行拦截
    @Bean
    public JwtUrlSecurityInterceptor jwtUrlSecurityInterceptorBean() throws Exception{
        return new JwtUrlSecurityInterceptor();
    }

    @Autowired
    private MyAuthenticationManager myAuthenticationManager;

    @Autowired
    private TokenAuthenticationService tokenAuthenticationService;

    @Autowired
    private LogoutHandler logoutHandler;

    @Override
    protected  void configure(HttpSecurity http)throws  Exception{
        http.csrf().disable().authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers("/").permitAll()
                .antMatchers("/user/**").permitAll()
                .antMatchers("/login.html").permitAll()
                .antMatchers("/index.html").permitAll()
                .antMatchers("/test").hasAuthority("USER")
                .anyRequest().authenticated()
//                .antMatchers("/test").hasRole("USER")
                .and()
                .logout().addLogoutHandler(logoutHandler).logoutSuccessUrl("/login.html").permitAll()
                .and()
                // We filter the api/login requests
                .addFilterBefore(jwtUrlSecurityInterceptorBean(), FilterSecurityInterceptor.class)
                .addFilterBefore(new JWTLoginFilter("/**", authenticationManager(), tokenAuthenticationService),
                        UsernamePasswordAuthenticationFilter.class)
                // And filter other requests to check the presence of JWT in header
                .addFilterBefore(jwtAuthenticationFilter(),
                        UsernamePasswordAuthenticationFilter.class);


    }

    public JWTAuthenticationFilter jwtAuthenticationFilter(){
        return new JWTAuthenticationFilter(tokenAuthenticationService);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // Create a default account
        auth.authenticationProvider(myAuthenticationManager);
    }
}
